{* $Id$ *}
{include_core file="admin/lib/header.html" title="优惠券展示"}
<div class="page-header border-bottom clearfix">
  <h2 class="left">优惠券管理<span>{if $info}编辑{else}添加{/if}优惠券</span></h2>
  <a href="{AnUrl('coupon/admin')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回优惠券列表</a>
</div>
<!--start:优惠券新增-->
<div class="add-form">
  <form class="js_creatForm" action="{AnUrl('coupon/admin/save')}" method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="{$coupon_info.id}">
    <input type="hidden" name="info[options_type]" value="{$options_type}">
    <table class="table" cellpadding="0" cellspacing="0">
    {if $options_type == 1}
      <tr>
        <th><label for="" class="add-f-label">所属分类：</label></th>
        <td>
          <div class="js_parent">
            {$parent_path = explode('-',$category_info.path)}
            {foreach $parent_path as $k=>$v}
            <select class="add-f-slt1 js_select js_sel_0" >
              {if $k == 0}
              <option value="0">全部分类</option>
              {else}
              <option value="{$parent_path.$k}">请选择分类</option>
              {/if}
              {$category_parent_list = category_helper::get_category_list_by_parent_id($v,$category_info.res_name)}
              {foreach $category_parent_list as $key=>$value}
              {$curr_id = $parent_path[$k+1]}
              {if !$curr_id}
                {$curr_id = $coupon_info.category_id}
              {/if}
              <option value="{$value.id}" {if $value.id == $curr_id}selected{/if}>{$value.title}</option>
              {/foreach}
            </select>
            {/foreach}
          </div>
          <input type="hidden" class="parent_id js_parent_id" name="info[category_id]" value="{$coupon_info.category_id}" empty-data="{if $category_info}1{/if}" path-data="{$category_info.path}">
          <div class="tips">
            <span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>
            请选择所属分类
          </div>
        </td>
      </tr>
      {else}
      <tr>
        <th><label for="" class="add-f-label">所属商品：</label></th>
        <td>
          <div class="js_parent">
            <select class="add-f-slt1 js_select js_sel_1" name="info[goods_id]">
            <option value="0">全部商品</option>
            {foreach $goods_list as $k => $v}
              <option {if $coupon_info.goods_id == $v.id}selected{/if} value="{$v.id}">{$v.title}---ID：{$v.id}</option>
            {/foreach}
            </select>
          </div>
          <div class="tips">
            <span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>
            请选择所属商品
          </div>
        </td>
      </tr>
      {/if}
      <tr>
        <th><label for="" class="add-f-label">优惠券标题：</label></th>
        <td>
          <input name="info[title]" style="width:490px;" value="{$coupon_info.title}" type="text" class="add-f-ipt1 js_title">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>优惠券标题会显示在相应内容的标题位置，最多输入 <span style="color:red;">20</span> 字，还可输入 <span class="js_titleNum" style="color:red;">20</span> 字</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">使用最低价格：</label></th>
        <td>
          <input name="info[limit_price]" style="width:490px;" value="{$coupon_info.limit_price}" type="text" class="add-f-ipt1 js_price">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>优惠券使用最低满足价格, 默认为0</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">优惠价格：</label></th>
        <td>
          <input name="info[offer_price]" style="width:490px;" value="{$coupon_info.offer_price}" type="text" class="add-f-ipt1 js_offerPrice js_price">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>优惠券价格是优惠可以抵用现金的价格</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">优惠数量：</label></th>
        <td>
          <input name="info[total]" style="width:490px;" type="text" value="{$coupon_info.total}" class="add-f-ipt1 js_total js_number" {if $id}disabled{/if}>
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>该优惠券总共有多少张</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">多少积分可兑换：</label></th>
        <td>
          <input name="info[score]" style="width:490px;" value="{$coupon_info.score}" type="text" class="add-f-ipt1 js_number">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>多少积分可兑换当前优惠券, 默认为0</div>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">优惠有效期：</label></th>
        <td>
          <input type="text" class="search-f-ipt1 search-f-ipt2" style="width:160px"  name="info[start_time]" value="{$coupon_info.start_time}" id="startTime">
          <span class="search-f-line"></span>
          <input type="text" class="search-f-ipt1 search-f-ipt2" style="width:160px" name="info[end_time]" value="{$coupon_info.end_time}" id="stopTime">
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>选择优惠券的有效日期</div>
        </td>
      </tr>
      {if $options_type == 2}
      <tr>
          <th valign="top"><span class="add-f-label">是否所有人都可以使用：</span></th>
          <td>
            <label class="add-f-label">
              <input type="radio" name="info[is_all_user]" value="1" class="add-f-radio" {if $coupon_info.is_all_user == 1}checked{/if}>允许
            </label>&nbsp;&nbsp;
            <label class="add-f-label">
              <input type="radio" name="info[is_all_user]" value="0" class="add-f-radio" {if $coupon_info.is_all_user == 0}checked{/if}>不允许
            </label>
          </td>
      </tr>
      {/if}
      <tr><th>&nbsp;</th><td class="noborder"><button type="submit" class="btn btn-success js_submit">&nbsp;&nbsp;&nbsp;确认&nbsp;&nbsp;&nbsp;</button></td></tr>
    </table>
  </form>
</div>
<!--end:优惠券新增-->
{load_js file='zclip/jquery.zclip.min.js'}
{load_js file='zclip/ZeroClipboard.js'}
<script>
var resName = 'coupon';
function setNum(thisClass, maxNum) {
  var presentNum = $('.'+thisClass).val().length;
  var remainNum  = maxNum - presentNum;
  if (remainNum < 0) {
    $('.'+thisClass).val($('.'+thisClass).val().substr(0,maxNum));
    $('.'+thisClass+'Num').text(0);
  } else {
    $('.'+thisClass+'Num').text(remainNum);
  }
}


// ajax获取分类列表
var currId   = $('.js_currId').val();
var res_name = 'goods';
var parent_id = 0;
var selNum = 0;
function getCategoryList(res_name, parent_id, firstSel){
  $.getJSON("{AnUrl('category/admin/check_menu')}", { res_name:res_name, parent_id:parent_id }, function(data){
    if (firstSel) {
      selNum = 0;
      $('.js_sel_'+selNum).nextAll().remove();
      $('.js_sel_'+selNum).html('<option value="0">全部分类</option>');
    } else if (data.info == 'ok' && data.category_list != '') {
      selNum++;
      $('.js_parent').append('<select class="add-f-slt1 js_select js_sel_'+selNum+'"></select>');
      $('.js_sel_'+selNum).append('<option value="'+parent_id+'">请选择..</option>');
    }
    if (data.info == 'ok' && data.category_list != '') {
      var optionCount = data.category_list.length;
      for (var i = 0; i < optionCount; i++) {
        if (currId != data['category_list'][i].id) {
          $('.js_sel_'+selNum).append('<option value="'+data['category_list'][i].id+'">'+data['category_list'][i].title+'</option>');
        }
      };
    };
  });
}

$(function(){

  //初始化分类列表
  var res_name = "goods";
  if ($('.js_parent_id').attr('empty-data') != 1) {
    getCategoryList(res_name, 0, 1);
  }


  //选择分类调取子分类
  $('.js_parent').on('change', '.js_select', function(){
    var currParent = $(this).val();
    var firstValue = $(this).children().eq(0).attr('value');
    $('.js_parent_id').val(currParent);
    $(this).nextAll().remove();
    if (firstValue != currParent) {
      getCategoryList(res_name, currParent, 0);
    };
  })

  // 初始化剩余字数
  setNum('js_title', 20);
  $('.js_title').keyup(function(){
    setNum('js_title', 20);
  });

  // 输入数字
  $('.js_number').keyup(function(){
    autoNum($(this));
  });

  // 选择时间
  // $('#startTime, #stopTime').datepicker({
  //   dateFormat: 'yy-mm-dd',
  //   showButtonPanel: true,
  //   changeYear:true,
  //   changeMonth:true,
  //   minDate: '-10y',
  //   maxDate: '+5y',
  //   yearSuffix: ''
  // });
  $('#startTime, #stopTime').datetimepicker({
    currentText: '当前时间',
    closeText: '确定',
    timeText: '时间',
    hourText: '时',
    minuteText: '分',
    secondText: null,
    timeFormat: "HH:mm",
    dateFormat: "yy-mm-dd"
  })
  
  // 提交验证
  $('.js_submit').click(function(e){
    e.preventDefault();
    var title      = $.trim($('.js_title').val());
    var offerPrice = $.trim($('.js_offerPrice').val());
    var total      = $.trim($('.js_total').val());
    var startTime  = $.trim($('#startTime').val());
    var stopTime   = $.trim($('#stopTime').val());

    if (!title) {
      showMsg('标题不能为空');
      return;
    }

    if (!offerPrice) {
      showMsg('优惠券价格不能为空');
      return;
    }

    if (!total) {
      showMsg('优惠券数量不能为空');
      return;
    }

    if (startTime > stopTime) {
      showMsg('结束时间应大于等于开始时间');
      return;
    }

    $('.js_creatForm').submit();
  });
});

// 自动转为数字
function autoNum(obj) {
  var currNum = obj.val();
  currNum = parseInt(currNum);
  if (isNaN(currNum)) {
    currNum = '';
  };
  obj.val(currNum);
}
</script>
{include_core file="admin/lib/footer.html"}